#include <iostream>
#include <vector>
#include <algorithm> // For std::min
#include <limits>    // For std::numeric_limits

using namespace std;

class Solution 
{
public:
    int minSubArrayLen(int target, std::vector<int>& nums) 
    {
        int n = nums.size();
        if (n == 0) 
        {
            return 0;
        }

        int start = 0;
        int current_sum = 0;
        // 初始化为一个不可能达到的最大值
        int min_length = std::numeric_limits<int>::max();

        // end 指针负责扩大窗口
        for (int end = 0; end < n; end++) 
        {
            current_sum += nums[end];

            // 当窗口内的和满足条件时，开始缩小窗口
            while (current_sum >= target) 
            {
                // 更新最小长度
                min_length = std::min(min_length, end - start + 1);
                
                // 尝试缩小窗口
                current_sum -= nums[start];
                start++;
            }
        }

        // 如果 min_length 没有被更新过，说明没有找到满足条件的子数组
        return (min_length == std::numeric_limits<int>::max()) ? 0 : min_length;
    }
};

int main()
{
    Solution s;
    
    // 测试案例1：基本测试案例
    vector<int> nums1 = {2, 3, 1, 2, 4, 3};
    int target1 = 7;
    cout << "原始数组: ";
    for(int num : nums1) {
        cout << num << " ";
    }
    cout << endl;
    cout << "目标值: " << target1 << endl;
    
    int result1 = s.minSubArrayLen(target1, nums1);
    cout << "最小子数组长度: " << result1 << endl; // 输出: 2 (子数组 [4,3])
    
    // 测试案例2：找不到满足条件的子数组
    vector<int> nums2 = {1, 1, 1, 1, 1, 1, 1, 1};
    int target2 = 11;
    cout << "\n原始数组: ";
    for(int num : nums2) {
        cout << num << " ";
    }
    cout << endl;
    cout << "目标值: " << target2 << endl;
    
    int result2 = s.minSubArrayLen(target2, nums2);
    cout << "最小子数组长度: " << result2 << endl; // 输出: 0 (找不到满足条件的子数组)
    
    return 0;
}
